﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Controllers
{
    public class HobbiesAjaxCheckBoxListController : Controller
    {
        private readonly IRepo<Hobby> r;

        public HobbiesAjaxCheckBoxListController(IRepo<Hobby> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(IEnumerable<int> keys)
        {
            keys = keys ?? new List<int>();
            var res = r.GetAll().Take(5).Select(o =>
                                                new SelectListItem
                                                    {
                                                        Text = o.Name,
                                                        Value = o.Id.ToString(),
                                                        Selected = (keys.Contains(o.Id))
                                                    });

            return Json(res);
        }
    }

    public class ShowsAjaxCheckBoxListController : Controller
    {
        private readonly IRepo<Show> r;

        public ShowsAjaxCheckBoxListController(IRepo<Show> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(IEnumerable<int> keys)
        {
            keys = keys ?? new List<int>();
            var res = r.GetAll().Select(o =>
                                                new SelectListItem
                                                    {
                                                        Text = o.Name,
                                                        Value = o.Id.ToString(),
                                                        Selected = (keys.Contains(o.Id))
                                                    });

            return Json(res);
        }
    }

    public class CharactersAjaxCheckBoxListController : Controller
    {
        private readonly IRepo<Person> r;

        public CharactersAjaxCheckBoxListController(IRepo<Person> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(IEnumerable<int> keys, IEnumerable<int> parent)
        {
            if(parent == null) return new EmptyResult();
            keys = keys ?? new List<int>();
            
            var res = r.GetAll().Where(o => o.Show != null && parent.Contains(o.Show.Id))
                .Select(o => 
                                                new SelectListItem
                                                    {
                                                        Text = o.FirstName + " " + o.LastName,
                                                        Value = o.Id.ToString(),
                                                        Selected = (keys.Contains(o.Id))
                                                    });

            return Json(res);
        }
    }
}